package com.blb.controller;


import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Brand;
import com.blb.entity.BrandType;
import com.blb.entity.Comtype;
import com.blb.mapper.ComtypeMapper;
import com.blb.service.IBrandService;
import com.blb.service.IBrandTypeService;
import com.blb.utils.LayuiPage;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuzhuo
 * @since 2019-07-10
 */
@Controller
@RequestMapping("brand")
public class BrandController {
	
	@Autowired
	private IBrandService brandService;
	
	@Autowired
	private ComtypeMapper comtypeMapper;
	
	@Autowired
	private IBrandTypeService brandTypeService;
	
	/**
	 * 跳转品牌界面
	 * @return
	 */
	@RequestMapping("index")
	public String getIndex(){
		return "admin/brand/brand_list";
	}
	
	/**
	 * 分页
	 * @param page
	 * @param limit
	 * @param markname
	 * @return
	 */
	@RequestMapping("page")
	@ResponseBody
	public LayuiPage page(int page,int limit,String markname){
		Page<Brand> page2 = new Page<Brand>((page-1)*limit,limit);
		IPage<Brand> ipage = null;
		ipage = brandService.page(page2, null);
		
		if(markname!=null && !markname.equals("")){
			ipage = brandService.page(page2, new QueryWrapper<Brand>().like("markname", markname));
		}
		LayuiPage layuiPage = new LayuiPage();
		layuiPage.setCount((int) ipage.getCurrent());
		layuiPage.setData(ipage.getRecords());
		
		return layuiPage;
	}
	
	@RequestMapping("showAdd")
	public String showAdd(Model model){
		List<Comtype> comtypes = comtypeMapper.selectByExample(null);
		model.addAttribute("comtypes", comtypes);
		return "admin/brand/brand_add";
	}
	
	/**
	 * 添加
	 * @return
	 */
	@RequestMapping("save")
	@ResponseBody
	public String save(Brand brand,@Param("comtypes")String comtypes){
		
		try {
			brandService.add(brand, comtypes);
		} catch (Exception e) {
			e.printStackTrace();
			e.getMessage();
		}
		return "";
	}
	
	@RequestMapping("isExist")
	@ResponseBody
	public String isExist(String markname){
		Brand brand = brandService.getOne(new QueryWrapper<Brand>().eq("markname", markname));
		if(brand!=null){
			return "品牌名已经存在";
		}
		return "";
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String delete(@RequestBody List<String> ids){
		try {
			brandService.delete(ids);
		} catch (Exception e) {
			e.printStackTrace();
			e.getMessage();
		}
		return "";
	}
	
	@RequestMapping("showUpdate")
	public String showudpate(Brand brand,Model model){
		List<Comtype> list = comtypeMapper.selectByExample(null);
		model.addAttribute("comtypes", list);
		
		List<BrandType> list2 = brandTypeService.getBrandTypeByBrand(brand.getId());
		
		String choiseRoles = "";
		if(!list2.isEmpty()){
			choiseRoles = JSON.toJSON(list2).toString();
		}
		
		model.addAttribute("choiseRoles", choiseRoles);
		model.addAttribute("brand", brand);
		return "admin/brand/brand_update";
	}
	
	@RequestMapping("update")
	@ResponseBody
	public String update(Brand brand,String comtypes){
		try {
			brandService.update(brand, comtypes);
		} catch (Exception e) {
			e.printStackTrace();
			e.getMessage();
		}
		return "";
		
	}
}

